package com.simplesoftwares.client.library.composite;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.InlineLabel;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.mywidgets.NumbersOnly;
import com.slicktechnologies.shared.common.productlayer.Tax;

// TODO: Auto-generated Javadoc
/**
 * The Class TaxComposite.
 */
public class TaxComposite extends Composite implements ValueChangeHandler<Boolean>,HasValue<Tax>,CompositeInterface
,Editor<Tax>
{
	
	/** The sellbl. */
	public InlineLabel taxlbl,sellbl;
	
	/** The taxch. */
	public CheckBox taxch;
	
	/** The percentagelbl. */
	public InlineLabel percentagelbl;
	
	/** The percentage box. */
	public DoubleBox percentageBox;
	
	/** The tax inlcusive. */
	public InlineLabel taxInlcusive;
	
	/** The tax inclusivech. */
	public CheckBox taxInclusivech;
	
	
	
	/** The table. */
	public Grid table;
	
	/**
	 * Instantiates a new tax composite.
	 */
	public TaxComposite()
	{
		sellbl=new InlineLabel("Select");
		taxch=new CheckBox("");
		taxInclusivech=new CheckBox();
		taxInlcusive=new InlineLabel("Inclusive");
		percentageBox=new DoubleBox();
		percentageBox.addKeyPressHandler(new NumbersOnly());
		percentagelbl=new InlineLabel("%");
		taxInclusivech=new CheckBox();
		
		table=new Grid(1, 6);
		
		
		table.setWidget(0, 0, sellbl);
		table.setWidget(0, 1,taxch);
		table.setWidget(0, 2, percentagelbl);
		table.setWidget(0, 3, percentageBox);
		table.setWidget(0, 4, taxInlcusive);
		table.setWidget(0, 5, taxInclusivech);
		
		//Why this was needed?
		percentageBox.addStyleName("percentage");
		
		
		
		initWidget(table);
		
		taxch.addValueChangeHandler(this);
		taxch.setValue(false);
		percentageBox.setEnabled(false);
		
	}

	
	
	



	/* (non-Javadoc)
	 * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent)
	 */
	@Override
	public void onValueChange(ValueChangeEvent<Boolean> event) {
		
		
		if(event.getValue()==true)
			percentageBox.setEnabled(true);
			
		else
		{
			percentageBox.setEnabled(false);
			percentageBox.setValue(null);
		}
		
	}
		

/* (non-Javadoc)
 * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
 */
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Tax> handler) {
	// TODO Auto-generated method stub
	return null;
}

/* (non-Javadoc)
 * @see com.google.gwt.user.client.ui.HasValue#getValue()
 */
@Override
public Tax getValue() 
{
	Tax t=new Tax();
	
	if(percentageBox.getValue()==null)
		t.setPercentage(0);
	else
		t.setPercentage(percentageBox.getValue());
	
	t.setInclusive(taxInclusivech.getValue());
	
	
	return t;
}

/* (non-Javadoc)
 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
 */
@Override
public void setValue(Tax value) 
{
	if(value!=null)
	{
		if(value.getPercentage()!=0)
		{
		this.taxch.setValue(true);
		percentageBox.setEnabled(true);
		this.percentageBox.setValue(value.getPercentage());
		taxInclusivech.setValue(value.isInclusive());
		}
		
		else
		{
			this.taxch.setValue(false);
			percentageBox.setValue(null);
			percentageBox.setEnabled(false);
			
		}
	}
	
	
	
}


/* (non-Javadoc)
 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
 */
@Override
public void setValue(Tax value, boolean fireEvents) {
	// TODO Auto-generated method stub
	
}


/* (non-Javadoc)
 * @see com.simplesoftwares.client.library.composite.CompositeInterface#setEnable(boolean)
 */
@Override
public void setEnable(boolean state)
{
	taxch.setEnabled(state);
	
	if(taxch.getValue()==false)
		percentageBox.setEnabled(false);
	else
		percentageBox.setEnabled(state);
	
	taxInclusivech.setEnabled(state);
}

/* (non-Javadoc)
 * @see com.simplesoftwares.client.library.composite.CompositeInterface#clear()
 */
@Override
public void clear()
{
	taxch.setValue(false);
	taxInclusivech.setValue(false);
	percentageBox.setValue(null);
	percentageBox.setEnabled(false);
}







/* (non-Javadoc)
 * @see com.simplesoftwares.client.library.composite.CompositeInterface#validate()
 */
@Override
public boolean validate() {
	
	return true;
}






















}
